<template>
	<scroll-view scroll-y class="container" :style="'height: '+height+'px;'" refresher-enabled="true"
		:refresher-triggered="triggered" @refresherrefresh="refresherrefresh" @scrolltolower="loadMoreData">
		<block  v-for="(item,index) in items">
			<post-list-cell class="forum_post_cell_content" :key="index" :item="item">
			
			</post-list-cell>
			<view class="subscribe_message_content app_subtitle_color" v-if="!isSubscription && index==1">
				<view>订阅通知</view>
				<view class="subscribe_message_desc">其他用户在小程序中回复了您的消息会通知到您</view>
				<view class="subscribe_message_button_content" @tap="subscribeMessage">立即订阅</view>
			</view>
		</block>
		
	</scroll-view>
</template>

<script>
	import util from "../utils/util.js"
	import http from "../utils/http.js"

	export default {
		name: "post-list-view",
		props: {
			showSubscription:{
				type:Boolean,
				default:false
			},
			sortby: {
				type: String,
				default: ""
			},
			sortby: {
				type: String,
				default: ""
			},
			moduleId: {
				type: Number,
				default: 2
			},
			boardId: {
				type: String,
				default: "all"
			},
			url: {
				type: String,
				default: "forum/topiclist"
			},
			height: {
				type: Number,
				default: 200
			}
		},
		data() {
			return {
				items: [],
				triggered: false,
				isSubscription: true
			};
		},
		mounted() {
			this.loadData()

			//#ifdef MP-WEIXIN
			// if(this.showSubscription){
			// 	let that = this
			// 	wx.getSetting({
			// 		withSubscriptions: true,
			// 		success(res) {
			// 			console.log(res)
			// 			var itemSettings = res.subscriptionsSetting.itemSettings
			// 			if (itemSettings) {
			// 				if (itemSettings['YRPZ0aBUnxkQAyUgNowPFx3Cmun3mQlJ-JhnGBVNwso'] === 'accept') {
			// 					console.log('is accredit：ok')
			// 					that.isSubscription = true
			// 				} else {
			// 					that.isSubscription = false
			// 				}
			// 			}
			// 		}
			// 	})
			// }
			
			//#endif
		},
		methods: {
			subscribeMessage() {
				wx.requestSubscribeMessage({
					tmplIds: ['YRPZ0aBUnxkQAyUgNowPFx3Cmun3mQlJ-JhnGBVNwso'],
					success(res) {
						console.log(res)
					},
					fail(err) {
						console.log(err)
					}
				})
			},
			refresherrefresh() {
				this.triggered = true
				this.loadData()
			},
			loadData() {
				var that = this;
				http.request(this.url, {
					pageSize: 20,
					page: 1,
					boardId: this.boardId,
					sortby: this.sortby,
					moduleId: this.moduleId
				}, {
					success: function(data) {
						uni.stopPullDownRefresh();
						that.items = data.list.map(function(item) {
							item.showTime = util.dateStringFromTimestamp(item.last_reply_date);
							return item;
						})
						that.page = 2;
						that.triggered = false
					},
					fail: function(msg) {
						uni.stopPullDownRefresh();
						uni.showToast({
							title: msg,
							icon: "none"
						})
						that.triggered = false
					}
				})
			},
			loadMoreData() {
				var that = this;
				http.request(this.url, {
					pageSize: 20,
					page: this.page,
					boardId: this.boardId,
					sortby: this.sortby,
					moduleId: this.moduleId
				}, {
					success: function(data) {
						var items = that.items.concat(data.list.map(function(item) {
							item.showTime = util.dateStringFromTimestamp(item.last_reply_date);
							return item;
						}))
						that.items = items;
						that.page++;
					},
					fail: function(msg) {
						uni.showToast({
							title: msg,
							icon: "none"
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.subscribe_message_content {
		display: flex;
		flex-direction: column;
		align-items: center;
		color: #fff;
		font-size: 30upx;
		padding: 40upx;
	}

	.subscribe_message_desc {
		margin-top: 20upx;
		color: #aaaaaa;
		font-size: 26upx;
		text-align: center;
	}

	.subscribe_message_button_content {
		display: flex;
		justify-content: center;
		align-items: center;
		background-color: $uni-global-green-color;
		color: #fff;
		font-size: 30upx;
		height: 60upx;
		border-radius: 6upx;
		margin-right: 30upx;
		margin-left: 30upx;
		padding-left: 30upx;
		padding-right: 30upx;
		margin-top: 50upx;
	}
</style>
